<template>
  <div class="w">
    <div class="header">
      <div class="header_top">
        <i class="iconfont icon-fanhui" @click="back"></i>
        <span class="title">个人中心</span>
      </div>
      <div class="header_bottom">
        <div class="bottom_avatar" v-if="token == null">
          <el-image style="border-radius: 50px; width: 100%; height: 100%" :src="'initial.jpg'" fit="cover"></el-image>
        </div>
        <div class="bottom_avatar" v-if="token != null">
          <el-image style="border-radius: 50px; width: 100%; height: 100%" :src="userInfo.avatar" fit="cover"></el-image>
        </div>
        <div class="bottom_info" v-if="token == null">
          <div class="info_name" @click="login">
            <span>请登录</span>
          </div>
        </div>
        <div class="bottom_info" v-if="token != null">
          <div class="info_name">
            <span>{{ userInfo.name }}</span>
          </div>
          <div class="info_sex" v-if="userInfo.sex != null" :class="userInfo.sex == 0 ? 'info_nan' : 'info_nv'">
            <i class="iconfont icon-nvxing" v-if="userInfo.sex == 1"></i>
            <i class="iconfont icon-nanxing1" v-if="userInfo.sex == 0"></i>
          </div>
        </div>
      </div>
    </div>
    <div class="info">
      <div class="info_card">
        <div class="card_item" @click="goNext('orders')">
          <i class="iconfont icon-dingdan"></i>
          <span>订单管理</span>
          <i class="iconfont icon-jinrujiantou"></i>
        </div>
        <div class="card_item" @click="goNext('address')">
          <i class="iconfont icon-dingwei-copy"></i>
          <span>地址管理</span>
          <i class="iconfont icon-jinrujiantou"></i>
        </div>
      </div>
      <div class="info_card">
        <div class="card_item" @click="goNext('collect')">
          <i class="iconfont icon-wodeshoucang"></i>
          <span>我的收藏</span>
          <i class="iconfont icon-jinrujiantou"></i>
        </div>
        <div class="card_item" @click="goNext('signUp')">
          <i class="iconfont icon-wodebaoming"></i>
          <span>我的报名</span>
          <i class="iconfont icon-jinrujiantou"></i>
        </div>
        <div class="card_item" @click="goNext('setting')">
          <i class="iconfont icon-shezhi"></i>
          <span>设置</span>
          <i class="iconfont icon-jinrujiantou"></i>
        </div>
      </div>
      <div class="button" @click="logout">
        <el-button>退出登录</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      info: [],
      userInfo: [],
      token: '',
    };
  },

  created() {
    this.info = JSON.parse(window.localStorage.getItem('userInfo'));
    this.token = sessionStorage.getItem('token');
    if (this.token != null) {
      this.getUserInfo();
    }
  },

  methods: {
    login() {
      this.$router.push('/login');
    },
    back() {
      this.$router.push('/');
    },
    async getUserInfo() {
      const { data: res } = await this.$http.get('user/getById/' + this.info.id);
      if (res.code == 1) {
        this.userInfo = res.data;
        return;
      } else {
        return this.$message.error('信息获取失败');
      }
    },
    goNext(url) {
      if (url == 'orders') {
        this.$router.push('/orders');
      }
      if (url == 'address') {
        this.$router.push('/address');
      }
      if (url == 'collect') {
        this.$router.push('/collect');
      }
      if (url == 'signUp') {
        this.$router.push('/mySignUp');
      }
      if (url == 'setting') {
        this.$router.push('/setting');
      }
    },
    logout() {
      window.localStorage.removeItem('userInfo');
      window.sessionStorage.removeItem('token');
      this.$router.push('/');
    },
  },
};
</script>

<style lang="scss" scoped>
.w {
  height: 100vh;
  width: 100%;
  .header {
    height: 140px;
    background-color: #ffc200;
    padding: 15px;
    .header_top {
      color: #333323;
      text-align: center;
      position: relative;
      margin-bottom: 20px;
      .icon-fanhui {
        position: absolute;
        font-size: 25px;
        line-height: 50px;
        left: 0px;
      }
      .title {
        font-size: 20px;
        line-height: 50px;
      }
    }
    .header_bottom {
      display: flex;
      .bottom_avatar {
        height: 60px;
        width: 60px;
        border-radius: 50px;
        margin-right: 20px;
      }
      .bottom_info {
        display: flex;
        align-items: center;
        .info_name {
          font-size: 20px;
          margin-right: 10px;
          letter-spacing: 1px;
        }
        .info_sex {
          border-radius: 50px;
          height: 20px;
          width: 20px;

          display: flex;
          align-items: center;
          justify-content: space-evenly;
          .iconfont {
            font-size: 15px;
            line-height: 20px;
            color: #fff;
            font-weight: bold;
          }
        }
        .info_nan {
          background-color: #30abff;
        }
        .info_nv {
          background-color: #ff4d94;
        }
      }
    }
  }
  .info {
    padding: 15px;
    background-color: #f3f2f7;
    height: calc(100vh - 200px);
    .info_card {
      height: auto;
      background-color: #ffffff;
      border-radius: 8px;
      padding-left: 15px;
      padding-right: 15px;
      margin-bottom: 15px;
      div:last-child {
        border: none;
      }
      .card_item {
        height: 60px;
        border-bottom: 2px solid #ebebeb;
        line-height: 60px;
        .iconfont {
          color: #ffc200;
          line-height: 60px;
          margin-right: 10px;
        }
        .icon-jinrujiantou {
          color: #000000;
          float: right;
        }
      }
    }
    .button {
      :deep(.el-button) {
        background-color: #fff;
        height: 50px;
        width: 100%;
        border-radius: 8px;
        font-size: 16px;
      }
    }
  }
}
</style>